﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms; 
using Einstein.Data;
using Einstein.Utility;
using DevComponents.DotNetBar;
using Einstein.Group.DAL;

namespace Einstein.Platform.Window.Config
{
    public partial class Config_Role : Office2007Form
    {
        public Config_Role()
        {
            InitializeComponent();
            BindRole();
        }


        void BindRole()
        {
            RoleInfoDAL dal = new RoleInfoDAL();
            dgList.PrimaryGrid.DataSource = dal.SelectAll();
            Config_Role_Edit.Enabled = false;
            Config_Role_Delete.Enabled = false;
        }

        private void Config_Role_New_Click(object sender, EventArgs e)
        {

            Config_Role_New form = new Config.Config_Role_New();
            form.Text = "新建用户角色";
            if (form.ShowDialog() == DialogResult.OK)
            {
                BindRole();
            }
        }

        RoleInfo currentData;
        public RoleInfo CurrentData
        {
            get
            {
                return currentData;
            }
            set
            {
                Config_Role_Edit.Enabled = true;
                Config_Role_Delete.Enabled = true;
                currentData = value;
            }
        }

        private void dgList_RowClick(object sender, DevComponents.DotNetBar.SuperGrid.GridRowClickEventArgs e)
        {
            RoleInfo currentItem = ((DevComponents.DotNetBar.SuperGrid.GridRow)e.GridRow).DataItem as RoleInfo;
            if (currentItem != null)
            {
                CurrentData = currentItem;
            }
        }

        private void Config_Role_Edit_Click(object sender, EventArgs e)
        {
            if (CurrentData != null)
            {
                if (CurrentData.IsSys.HasValue && CurrentData.IsSys.Value)
                {
                    BoxManager.ShowWarning("系统默认角色不能编辑！");
                    return;
                }
                Config_Role_New usertype_new = new Config_Role_New();
                usertype_new.Text = "编辑用户角色";
                usertype_new.RoleInfo = CurrentData;
                usertype_new.BindData();
                if (usertype_new.ShowDialog() == DialogResult.OK)
                {
                    BindRole();
                }
            }
            else
            {
                BoxManager.ShowWarning("您没有选择任何行，请重试。");
            }
        }

        private void Config_Role_Delete_Click(object sender, EventArgs e)
        {
            if (CurrentData != null)
            {
                if (CurrentData.IsSys.HasValue && CurrentData.IsSys.Value)
                {
                    BoxManager.ShowWarning("系统默认角色不能删除！");
                    return;
                }

                if (BoxManager.ShowDeleteAsk() != DialogResult.OK)
                {
                    return;
                }

                RoleInfoDAL dal = new RoleInfoDAL();
                bool result = dal.Delete(new RoleInfo { RoleID = CurrentData.RoleID });
                if (result)
                {
                    BindRole();
                    BoxManager.ShowDeleteSuccess();
                }
                else
                {
                    BoxManager.ShowDeleteFail();
                }
            }
            else
            {
                BoxManager.ShowWarning("您没有选择任何行，请重试。");
            }
        }
    }
}
